home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / pdc / libsrc / stringlib / tester.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-07  |  19.5 KB  |  630 lines

  1. _cli_parse(){return;}
  2. _wb_parse(){return;}
  3.  
  4. /*
  5.  * Test program for string(3) routines.
  6.  * 
  7.  * Note that at least one Bell Labs implementation of the string
  8.  * routines flunks a couple of these tests -- the ones which test
  9.  * behavior on "negative" characters.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include "string.h"
  14. #include "config.h"
  15.  
  16. #define    STREQ(a, b)    (strcmp((a), (b)) == 0)
  17.  
  18. char *it = "<UNSET>";        /* Routine name for message routines. */
  19. int waserror = 0;        /* For exit status. */
  20.  
  21. char uctest[] = "\004\203";    /* For testing signedness of chars. */
  22. int charsigned;            /* Result. */
  23.  
  24. /*
  25.  - check - complain if condition is not true
  26.  */
  27. int
  28. check(thing, number)
  29. int thing;
  30. int number;            /* Test number for error message. */
  31. {
  32.     if (!thing) {
  33.         printf("%s flunked test %ld\n", it, number);
  34.         waserror = 1;
  35.     }
  36. }
  37.  
  38. /*
  39.  - equal - complain if first two args don't strcmp as equal
  40.  */
  41. int
  42. equal(a, b, number)
  43. char *a;
  44. char *b;
  45. int number;            /* Test number for error message. */
  46. {
  47.     check(a != NULL && b != NULL && STREQ(a, b), number);
  48. }
  49.  
  50. char one[50];
  51. char two[50];
  52.  
  53. #ifdef UNIXERR
  54. #define ERR 1
  55. #endif
  56. #ifdef BERKERR
  57. #define ERR 1
  58. #endif
  59. #ifdef ERR
  60. int f;
  61. #ifdef unix
  62. extern char *sys_errlist[];
  63. extern int sys_nerr;
  64. #else
  65. char *sys_errlist[1] = {"dummy entry to keep compilers happy"};
  66. int sys_nerr = 1;
  67. #endif
  68. extern int errno;
  69. #endif
  70.  
  71. /* ARGSUSED */
  72. main(argc, argv)
  73. int argc;
  74. char *argv[];
  75. {
  76.     /*
  77.      * First, establish whether chars are signed.
  78.      */
  79.     if (uctest[0] < uctest[1])
  80.         charsigned = 0;
  81.     else
  82.         charsigned = 1;
  83.  
  84.     /*
  85.      * Then, do the rest of the work.  Split into two functions because
  86.      * some compilers get unhappy about a single immense function.
  87.      */
  88.     first();
  89.     second();
  90.  
  91.     exit((waserror) ? 1 : 0);
  92. }
  93.  
  94. first()
  95. {
  96.     /*
  97.      * Test strcmp first because we use it to test other things.
  98.      */
  99.     it = "strcmp";
  100.     check(strcmp("", "") == 0, 1);        /* Trivial case. */
  101.     check(strcmp("a", "a") == 0, 2);    /* Identity. */
  102.     check(strcmp("abc", "abc") == 0, 3);    /* Multicharacter. */
  103.     check(strcmp("abc", "abcd") < 0, 4);    /* Length mismatches. */
  104.     check(strcmp("abcd", "abc") > 0, 5);
  105.     check(strcmp("abcd", "abce") < 0, 6);    /* Honest miscompares. */
  106.     check(strcmp("abce", "abcd") > 0, 7);
  107.     check(strcmp("a\203", "a") > 0, 8);    /* Tricky if char signed. */
  108.     if (charsigned)                /* Sign-bit comparison. */
  109.         check(strcmp("a\203", "a\003") < 0, 9);
  110.     else
  111.         check(strcmp("a\203", "a\003") > 0, 9);
  112.  
  113.     /*
  114.      * Test strcpy next because we need it to set up other tests.
  115.      */
  116.     it = "strcpy";
  117.     check(strcpy(one, "abcd") == one, 1);    /* Returned value. */
  118.     equal(one, "abcd", 2);            /* Basic test. */
  119.  
  120.     /*int*/ strcpy(one, "x");
  121.     equal(one, "x", 3);            /* Writeover. */
  122.     equal(one+2, "cd", 4);            /* Wrote too much? */
  123.  
  124.     /*void*/ strcpy(two, "hi there");
  125.     /*void*/ strcpy(one, two);
  126.     equal(one, "hi there", 5);        /* Basic test encore. */
  127.     equal(two, "hi there", 6);        /* Stomped on source? */
  128.  
  129.     /*void*/ strcpy(one, "");
  130.     equal(one, "", 7);            /* Boundary condition. */
  131.  
  132.     /*
  133.      * strcat
  134.      */
  135.     it = "strcat";
  136.     /*void*/ strcpy(one, "ijk");
  137.     check(strcat(one, "lmn") == one, 1);    /* Returned value. */
  138.     equal(one, "ijklmn", 2);        /* Basic test. */
  139.  
  140.     /*void*/ strcpy(one, "x");
  141.     /*void*/ strcat(one, "yz");
  142.     equal(one, "xyz", 3);            /* Writeover. */
  143.     equal(one+4, "mn", 4);            /* Wrote too much? */
  144.  
  145.     /*void*/ strcpy(one, "gh");
  146.     /*void*/ strcpy(two, "ef");
  147.     /*void*/ strcat(one, two);
  148.     equal(one, "ghef", 5);            /* Basic test encore. */
  149.     equal(two, "ef", 6);            /* Stomped on source? */
  150.  
  151.     /*void*/ strcpy(one, "");
  152.     /*void*/ strcat(one, "");
  153.     equal(one, "", 7);            /* Boundary conditions. */
  154.     /*void*/ strcpy(one, "ab");
  155.     /*void*/ strcat(one, "");
  156.     equal(one, "ab", 8);
  157.     /*void*/ strcpy(one, "");
  158.     /*void*/ strcat(one, "cd");
  159.     equal(one, "cd", 9);
  160.  
  161.     /*
  162.      * strncat - first test it as strcat, with big counts, then
  163.      * test the count mechanism.
  164.      */
  165.     it = "strncat";
  166.     /*void*/ strcpy(one, "ijk");
  167.     check(strncat(one, "lmn", 99) == one, 1);    /* Returned value. */
  168.     equal(one, "ijklmn", 2);        /* Basic test. */
  169.  
  170.     /*void*/ strcpy(one, "x");
  171.     /*void*/ strncat(one, "yz", 99);
  172.     equal(one, "xyz", 3);            /* Writeover. */
  173.     equal(one+4, "mn", 4);            /* Wrote too much? */
  174.  
  175.     /*void*/ strcpy(one, "gh");
  176.     /*void*/ strcpy(two, "ef");
  177.     /*void*/ strncat(one, two, 99);
  178.     equal(one, "ghef", 5);            /* Basic test encore. */
  179.     equal(two, "ef", 6);            /* Stomped on source? */
  180.  
  181.     /*void*/ strcpy(one, "");
  182.     /*void*/ strncat(one, "", 99);
  183.     equal(one, "", 7);            /* Boundary conditions. */
  184.     /*void*/ strcpy(one, "ab");
  185.     /*void*/ strncat(one, "", 99);
  186.     equal(one, "ab", 8);
  187.     /*void*/ strcpy(one, "");
  188.     /*void*/ strncat(one, "cd", 99);
  189.     equal(one, "cd", 9);
  190.  
  191.     /*void*/ strcpy(one, "ab");
  192.     /*void*/ strncat(one, "cdef", 2);
  193.     equal(one, "abcd", 10);            /* Count-limited. */
  194.  
  195.     /*void*/ strncat(one, "gh", 0);
  196.     equal(one, "abcd", 11);            /* Zero count. */
  197.  
  198.     /*void*/ strncat(one, "gh", 2);
  199.     equal(one, "abcdgh", 12);        /* Count and length equal. */
  200.  
  201.     /*
  202.      * strncmp - first test as strcmp with big counts, then test
  203.      * count code.
  204.      */
  205.     it = "strncmp";
  206.     check(strncmp("", "", 99) == 0, 1);    /* Trivial case. */
  207.     check(strncmp("a", "a", 99) == 0, 2);    /* Identity. */
  208.     check(strncmp("abc", "abc", 99) == 0, 3);    /* Multicharacter. */
  209.     check(strncmp("abc", "abcd", 99) < 0, 4);    /* Length unequal. */
  210.     check(strncmp("abcd", "abc", 99) > 0, 5);
  211.     check(strncmp("abcd", "abce", 99) < 0, 6);    /* Honestly unequal. */
  212.     check(strncmp("abce", "abcd", 99) > 0, 7);
  213.     check(strncmp("a\203", "a", 2) > 0, 8);    /* Tricky if '\203' < 0 */
  214.     if (charsigned)                /* Sign-bit comparison. */
  215.         check(strncmp("a\203", "a\003", 2) < 0, 9);
  216.     else
  217.         check(strncmp("a\203", "a\003", 2) > 0, 9);
  218.     check(strncmp("abce", "abcd", 3) == 0, 10);    /* Count limited. */
  219.     check(strncmp("abce", "abc", 3) == 0, 11);    /* Count == length. */
  220.     check(strncmp("abcd", "abce", 4) < 0, 12);    /* Nudging limit. */
  221.     check(strncmp("abc", "def", 0) == 0, 13);    /* Zero count. */
  222.  
  223.     /*
  224.      * strncpy - testing is a bit different because of odd semantics
  225.      */
  226.     it = "strncpy";
  227.     check(strncpy(one, "abc", 4) == one, 1);    /* Returned value. */
  228.     equal(one, "abc", 2);            /* Did the copy go right? */
  229.  
  230.     /*void*/ strcpy(one, "abcdefgh");
  231.     /*void*/ strncpy(one, "xyz", 2);
  232.     equal(one, "xycdefgh", 3);        /* Copy cut by count. */
  233.  
  234.     /*void*/ strcpy(one, "abcdefgh");
  235.     /*void*/ strncpy(one, "xyz", 3);        /* Copy cut just before NUL. */
  236.     equal(one, "xyzdefgh", 4);
  237.  
  238.     /*void*/ strcpy(one, "abcdefgh");
  239.     /*void*/ strncpy(one, "xyz", 4);        /* Copy just includes NUL. */
  240.     equal(one, "xyz", 5);
  241.     equal(one+4, "efgh", 6);        /* Wrote too much? */
  242.  
  243.     /*void*/ strcpy(one, "abcdefgh");
  244.     /*void*/ strncpy(one, "xyz", 5);        /* Copy includes padding. */
  245.     equal(one, "xyz", 7);
  246.     equal(one+4, "", 8);
  247.     equal(one+5, "fgh", 9);
  248.  
  249.     /*void*/ strcpy(one, "abc");
  250.     /*void*/ strncpy(one, "xyz", 0);        /* Zero-length copy. */
  251.     equal(one, "abc", 10);    
  252.  
  253.     /*void*/ strncpy(one, "", 2);        /* Zero-length source. */
  254.     equal(one, "", 11);
  255.     equal(one+1, "", 12);    
  256.     equal(one+2, "c", 13);
  257.  
  258.     /*void*/ strcpy(one, "hi there");
  259.     /*void*/ strncpy(two, one, 9);
  260.     equal(two, "hi there", 14);        /* Just paranoia. */
  261.     equal(one, "hi there", 15);        /* Stomped on source? */
  262.  
  263.     /*
  264.      * strlen
  265.      */
  266.     it = "strlen";
  267.     check(strlen("") == 0, 1);        /* Empty. */
  268.     check(strlen("a") == 1, 2);        /* Single char. */
  269.     check(strlen("abcd") == 4, 3);        /* Multiple chars. */
  270.  
  271.     /*
  272.      * strchr
  273.      */
  274.     it = "strchr";
  275.     check(strchr("abcd", 'z') == NULL, 1);    /* Not found. */
  276.     /*void*/ strcpy(one, "abcd");
  277.     check(strchr(one, 'c') == one+2, 2);    /* Basic test. */
  278.     check(strchr(one, 'd') == one+3, 3);    /* End of string. */
  279.     check(strchr(one, 'a') == one, 4);    /* Beginning. */
  280.     check(strchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  281.     /*void*/ strcpy(one, "ababa");
  282.     check(strchr(one, 'b') == one+1, 6);    /* Finding first. */
  283.     /*void*/ strcpy(one, "");
  284.     check(strchr(one, 'b') == NULL, 7);    /* Empty string. */
  285.     check(strchr(one, '\0') == one, 8);    /* NUL in empty string. */
  286.  
  287.     /*
  288.      * index - just like strchr
  289.      */
  290.     it = "index";
  291.     check(index("abcd", 'z') == NULL, 1);    /* Not found. */
  292.     /*void*/ strcpy(one, "abcd");
  293.     check(index(one, 'c') == one+2, 2);    /* Basic test. */
  294.     check(index(one, 'd') == one+3, 3);    /* End of string. */
  295.     check(index(one, 'a') == one, 4);    /* Beginning. */
  296.     check(index(one, '\0') == one+4, 5);    /* Finding NUL. */
  297.     /*void*/ strcpy(one, "ababa");
  298.     check(index(one, 'b') == one+1, 6);    /* Finding first. */
  299.     /*void*/ strcpy(one, "");
  300.     check(index(one, 'b') == NULL, 7);    /* Empty string. */
  301.     check(index(one, '\0') == one, 8);    /* NUL in empty string. */
  302.  
  303.     /*
  304.      * strrchr
  305.      */
  306.     it = "strrchr";
  307.     check(strrchr("abcd", 'z') == NULL, 1);    /* Not found. */
  308.     /*void*/ strcpy(one, "abcd");
  309.     check(strrchr(one, 'c') == one+2, 2);    /* Basic test. */
  310.     check(strrchr(one, 'd') == one+3, 3);    /* End of string. */
  311.     check(strrchr(one, 'a') == one, 4);    /* Beginning. */
  312.     check(strrchr(one, '\0') == one+4, 5);    /* Finding NUL. */
  313.     /*void*/ strcpy(one, "ababa");
  314.     check(strrchr(one, 'b') == one+3, 6);    /* Finding last. */
  315.     /*void*/ strcpy(one, "");
  316.     check(strrchr(one, 'b') == NULL, 7);    /* Empty string. */
  317.     check(strrchr(one, '\0') == one, 8);    /* NUL in empty string. */
  318.  
  319.     /*
  320.      * rindex - just like strrchr
  321.      */
  322.     it = "rindex";
  323.     check(rindex("abcd", 'z') == NULL, 1);    /* Not found. */
  324.     /*void*/ strcpy(one, "abcd");
  325.     check(rindex(one, 'c') == one+2, 2);    /* Basic test. */
  326.     check(rindex(one, 'd') == one+3, 3);    /* End of string. */
  327.     check(rindex(one, 'a') == one, 4);    /* Beginning. */
  328.     check(rindex(one, '\0') == one+4, 5);    /* Finding NUL. */
  329.     /*void*/ strcpy(one, "ababa");
  330.     check(rindex(one, 'b') == one+3, 6);    /* Finding last. */
  331.     /*void*/ strcpy(one, "");
  332.     check(rindex(one, 'b') == NULL, 7);    /* Empty string. */
  333.     check(rindex(one, '\0') == one, 8);    /* NUL in empty string. */
  334. }
  335.  
  336. second()
  337. {
  338.     /*
  339.      * strpbrk - somewhat like strchr
  340.      */
  341.     it = "strpbrk";
  342.     check(strpbrk("abcd", "z") == NULL, 1);    /* Not found. */
  343.     /*void*/ strcpy(one, "abcd");
  344.     check(strpbrk(one, "c") == one+2, 2);    /* Basic test. */
  345.     check(strpbrk(one, "d") == one+3, 3);    /* End of string. */
  346.     check(strpbrk(one, "a") == one, 4);    /* Beginning. */
  347.     check(strpbrk(one, "") == NULL, 5);    /* Empty search list. */
  348.     check(strpbrk(one, "cb") == one+1, 6);    /* Multiple search. */
  349.     /*void*/ strcpy(one, "abcabdea");
  350.     check(strpbrk(one, "b") == one+1, 7);    /* Finding first. */
  351.     check(strpbrk(one, "cb") == one+1, 8);    /* With multiple search. */
  352.     check(strpbrk(one, "db") == one+1, 9);    /* Another variant. */
  353.     /*void*/ strcpy(one, "");
  354.     check(strpbrk(one, "bc") == NULL, 10);    /* Empty string. */
  355.     check(strpbrk(one, "") == NULL, 11);    /* Both strings empty. */
  356.  
  357.     /*
  358.      * strstr - somewhat like strchr
  359.      */
  360.     it = "strstr";
  361.     check(strstr("abcd", "z") == NULL, 1);    /* Not found. */
  362.     check(strstr("abcd", "abx") == NULL, 2);    /* Dead end. */
  363.     /*void*/ strcpy(one, "abcd");
  364.     check(strstr(one, "c") == one+2, 3);    /* Basic test. */
  365.     check(strstr(one, "bc") == one+1, 4);    /* Multichar. */
  366.     check(strstr(one, "d") == one+3, 5);    /* End of string. */
  367.     check(strstr(one, "cd") == one+2, 6);    /* Tail of string. */
  368.     check(strstr(one, "abc") == one, 7);    /* Beginning. */
  369.     check(strstr(one, "abcd") == one, 8);    /* Exact match. */
  370.     check(strstr(one, "abcde") == NULL, 9);    /* Too long. */
  371.     check(strstr(one, "de") == NULL, 10);    /* Past end. */
  372.     check(strstr(one, "") == one+4, 11);    /* Finding empty. */
  373.     /*void*/ strcpy(one, "ababa");
  374.     check(strstr(one, "ba") == one+1, 12);    /* Finding first. */
  375.     /*void*/ strcpy(one, "");
  376.     check(strstr(one, "b") == NULL, 13);    /* Empty string. */
  377.     check(strstr(one, "") == one, 14);    /* Empty in empty string. */
  378.     /*void*/ strcpy(one, "bcbca");
  379.     check(strstr(one, "bca") == one+2, 15);    /* False start. */
  380.     /*void*/ strcpy(one, "bbbcabbca");
  381.     check(strstr(one, "bbca") == one+1, 16);    /* With overlap. */
  382.  
  383.     /*
  384.      * strspn
  385.      */
  386.     it = "strspn";
  387.     check(strspn("abcba", "abc") == 5, 1);    /* Whole string. */
  388.     check(strspn("abcba", "ab") == 2, 2);    /* Partial. */
  389.     check(strspn("abc", "qx") == 0, 3);    /* None. */
  390.     check(strspn("", "ab") == 0, 4);    /* Null string. */
  391.     check(strspn("abc", "") == 0, 5);    /* Null search list. */
  392.  
  393.     /*
  394.      * strcspn
  395.      */
  396.     it = "strcspn";
  397.     check(strcspn("abcba", "qx") == 5, 1);    /* Whole string. */
  398.     check(strcspn("abcba", "cx") == 2, 2);    /* Partial. */
  399.     check(strcspn("abc", "abc") == 0, 3);    /* None. */
  400.     check(strcspn("", "ab") == 0, 4);    /* Null string. */
  401.     check(strcspn("abc", "") == 3, 5);    /* Null search list. */
  402.  
  403.     /*
  404.      * strtok - the hard one
  405.      */
  406.     it = "strtok";
  407.     /*void*/ strcpy(one, "first, second, third");
  408.     equal(strtok(one, ", "), "first", 1);    /* Basic test. */
  409.     equal(one, "first", 2);
  410.     equal(strtok((char *)NULL, ", "), "second", 3);
  411.     equal(strtok((char *)NULL, ", "), "third", 4);
  412.     check(strtok((char *)NULL, ", ") == NULL, 5);
  413.     /*void*/ strcpy(one, ", first, ");
  414.     equal(strtok(one, ", "), "first", 6);    /* Extra delims, 1 tok. */
  415.     check(strtok((char *)NULL, ", ") == NULL, 7);
  416.     /*void*/ strcpy(one, "1a, 1b; 2a, 2b");
  417.     equal(strtok(one, ", "), "1a", 8);    /* Changing delim lists. */
  418.     equal(strtok((char *)NULL, "; "), "1b", 9);
  419.     equal(strtok((char *)NULL, ", "), "2a", 10);
  420.     /*void*/ strcpy(two, "x-y");
  421.     equal(strtok(two, "-"), "x", 11);    /* New string before done. */
  422.     equal(strtok((char *)NULL, "-"), "y", 12);
  423.     check(strtok((char *)NULL, "-") == NULL, 13);
  424.     /*void*/ strcpy(one, "a,b, c,, ,d");
  425.     equal(strtok(one, ", "), "a", 14);    /* Different separators. */
  426.     equal(strtok((char *)NULL, ", "), "b", 15);
  427.     equal(strtok((char *)NULL, " ,"), "c", 16);    /* Permute list too. */
  428.     equal(strtok((char *)NULL, " ,"), "d", 17);
  429.     check(strtok((char *)NULL, ", ") == NULL, 18);
  430.     check(strtok((char *)NULL, ", ") == NULL, 19);    /* Persistence. */
  431.     /*void*/ strcpy(one, ", ");
  432.     check(strtok(one, ", ") == NULL, 20);    /* No tokens. */
  433.     /*void*/ strcpy(one, "");
  434.     check(strtok(one, ", ") == NULL, 21);    /* Empty string. */
  435.     /*void*/ strcpy(one, "abc");
  436.     equal(strtok(one, ", "), "abc", 22);    /* No delimiters. */
  437.     check(strtok((char *)NULL, ", ") == NULL, 23);
  438.     /*void*/ strcpy(one, "abc");
  439.     equal(strtok(one, ""), "abc", 24);    /* Empty delimiter list. */
  440.     check(strtok((char *)NULL, "") == NULL, 25);
  441.     /*void*/ strcpy(one, "abcdefgh");
  442.     /*void*/ strcpy(one, "a,b,c");
  443.     equal(strtok(one, ","), "a", 26);    /* Basics again... */
  444.     equal(strtok((char *)NULL, ","), "b", 27);
  445.     equal(strtok((char *)NULL, ","), "c", 28);
  446.     check(strtok((char *)NULL, ",") == NULL, 29);
  447.     equal(one+6, "gh", 30);            /* Stomped past end? */
  448.     equal(one, "a", 31);            /* Stomped old tokens? */
  449.     equal(one+2, "b", 32);
  450.     equal(one+4, "c", 33);
  451.  
  452.     /*
  453.      * memcmp
  454.      */
  455.     it = "memcmp";
  456.     check(memcmp("a", "a", 1) == 0, 1);    /* Identity. */
  457.     check(memcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  458.     check(memcmp("abcd", "abce", 4) < 0, 3);    /* Honestly unequal. */
  459.     check(memcmp("abce", "abcd", 4) > 0, 4);
  460.     check(memcmp("alph", "beta", 4) < 0, 5);
  461.     if (charsigned)                /* Sign-bit comparison. */
  462.         check(memcmp("a\203", "a\003", 2) < 0, 6);
  463.     else
  464.         check(memcmp("a\203", "a\003", 2) > 0, 6);
  465.     check(memcmp("abce", "abcd", 3) == 0, 7);    /* Count limited. */
  466.     check(memcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  467.  
  468.     /*
  469.      * memchr
  470.      */
  471.     it = "memchr";
  472.     check(memchr("abcd", 'z', 4) == NULL, 1);    /* Not found. */
  473.     /*void*/ strcpy(one, "abcd");
  474.     check(memchr(one, 'c', 4) == one+2, 2);    /* Basic test. */
  475.     check(memchr(one, 'd', 4) == one+3, 3);    /* End of string. */
  476.     check(memchr(one, 'a', 4) == one, 4);    /* Beginning. */
  477.     check(memchr(one, '\0', 5) == one+4, 5);    /* Finding NUL. */
  478.     /*void*/ strcpy(one, "ababa");
  479.     check(memchr(one, 'b', 5) == one+1, 6);    /* Finding first. */
  480.     check(memchr(one, 'b', 0) == NULL, 7);    /* Zero count. */
  481.     check(memchr(one, 'a', 1) == one, 8);    /* Singleton case. */
  482.     /*void*/ strcpy(one, "a\203b");
  483.     check(memchr(one, (char)0203, 3) == one+1, 9);    /* Unsignedness. */
  484.  
  485.     /*
  486.      * memcpy
  487.      *
  488.      * Note that X3J11 says memcpy must work regardless of overlap.
  489.      * The SVID says it might fail.
  490.      */
  491.     it = "memcpy";
  492.     check(memcpy(one, "abc", 4) == one, 1);    /* Returned value. */
  493.     equal(one, "abc", 2);            /* Did the copy go right? */
  494.  
  495.     /*void*/ strcpy(one, "abcdefgh");
  496.     /*void*/ memcpy(one+1, "xyz", 2);
  497.     equal(one, "axydefgh", 3);        /* Basic test. */
  498.  
  499.     /*void*/ strcpy(one, "abc");
  500.     /*void*/ memcpy(one, "xyz", 0);
  501.     equal(one, "abc", 4);            /* Zero-length copy. */
  502.  
  503.     /*void*/ strcpy(one, "hi there");
  504.     /*void*/ strcpy(two, "foo");
  505.     /*void*/ memcpy(two, one, 9);
  506.     equal(two, "hi there", 5);        /* Just paranoia. */
  507.     equal(one, "hi there", 6);        /* Stomped on source? */
  508.  
  509.     /*void*/ strcpy(one, "abcdefgh");
  510.     /*void*/ memcpy(one+1, one, 9);
  511.     equal(one, "aabcdefgh", 7);        /* Overlap, right-to-left. */
  512.  
  513.     /*void*/ strcpy(one, "abcdefgh");
  514.     /*void*/ memcpy(one+1, one+2, 7);
  515.     equal(one, "acdefgh", 8);        /* Overlap, left-to-right. */
  516.  
  517.     /*void*/ strcpy(one, "abcdefgh");
  518.     /*void*/ memcpy(one, one, 9);
  519.     equal(one, "abcdefgh", 9);        /* 100% overlap. */
  520.  
  521.     /*
  522.      * memccpy - first test like memcpy, then the search part
  523.      *
  524.      * The SVID, the only place where memccpy is mentioned, says
  525.      * overlap might fail, so we don't try it.  Besides, it's hard
  526.      * to see the rationale for a non-left-to-right memccpy.
  527.      */
  528.     it = "memccpy";
  529.     check(memccpy(one, "abc", 'q', 4) == NULL, 1);    /* Returned value. */
  530.     equal(one, "abc", 2);            /* Did the copy go right? */
  531.  
  532.     /*void*/ strcpy(one, "abcdefgh");
  533.     /*void*/ memccpy(one+1, "xyz", 'q', 2);
  534.     equal(one, "axydefgh", 3);        /* Basic test. */
  535.  
  536.     /*void*/ strcpy(one, "abc");
  537.     /*void*/ memccpy(one, "xyz", 'q', 0);
  538.     equal(one, "abc", 4);            /* Zero-length copy. */
  539.  
  540.     /*void*/ strcpy(one, "hi there");
  541.     /*void*/ strcpy(two, "foo");
  542.     /*void*/ memccpy(two, one, 'q', 9);
  543.     equal(two, "hi there", 5);        /* Just paranoia. */
  544.     equal(one, "hi there", 6);        /* Stomped on source? */
  545.  
  546.     /*void*/ strcpy(one, "abcdefgh");
  547.     /*void*/ strcpy(two, "horsefeathers");
  548.     check(memccpy(two, one, 'f', 9) == two+6, 7);    /* Returned value. */
  549.     equal(one, "abcdefgh", 8);        /* Source intact? */
  550.     equal(two, "abcdefeathers", 9);        /* Copy correct? */
  551.  
  552.     /*void*/ strcpy(one, "abcd");
  553.     /*void*/ strcpy(two, "bumblebee");
  554.     check(memccpy(two, one, 'a', 4) == two+1, 10);    /* First char. */
  555.     equal(two, "aumblebee", 11);
  556.     check(memccpy(two, one, 'd', 4) == two+4, 12);    /* Last char. */
  557.     equal(two, "abcdlebee", 13);
  558.     /*void*/ strcpy(one, "xyz");
  559.     check(memccpy(two, one, 'x', 1) == two+1, 14);    /* Singleton. */
  560.     equal(two, "xbcdlebee", 15);
  561.  
  562.     /*
  563.      * memset
  564.      */
  565.     it = "memset";
  566.     /*void*/ strcpy(one, "abcdefgh");
  567.     check(memset(one+1, 'x', 3) == one+1, 1);    /* Return value. */
  568.     equal(one, "axxxefgh", 2);        /* Basic test. */
  569.  
  570.     /*void*/ memset(one+2, 'y', 0);
  571.     equal(one, "axxxefgh", 3);        /* Zero-length set. */
  572.  
  573.     /*void*/ memset(one+5, '\0', 1);
  574.     equal(one, "axxxe", 4);            /* Zero fill. */
  575.     equal(one+6, "gh", 5);            /* And the leftover. */
  576.  
  577.     /*void*/ memset(one+2, (char)010045, 1);
  578.     equal(one, "ax\045xe", 6);        /* Unsigned char convert. */
  579.  
  580.     /*
  581.      * bcopy - much like memcpy
  582.      *
  583.      * Berklix manual is silent about overlap, so don't test it.
  584.      */
  585.     it = "bcopy";
  586.     /*void*/ bcopy("abc", one, 4);
  587.     equal(one, "abc", 1);            /* Simple copy. */
  588.  
  589.     /*void*/ strcpy(one, "abcdefgh");
  590.     /*void*/ bcopy("xyz", one+1, 2);
  591.     equal(one, "axydefgh", 2);        /* Basic test. */
  592.  
  593.     /*void*/ strcpy(one, "abc");
  594.     /*void*/ bcopy("xyz", one, 0);
  595.     equal(one, "abc", 3);            /* Zero-length copy. */
  596.  
  597.     /*void*/ strcpy(one, "hi there");
  598.     /*void*/ strcpy(two, "foo");
  599.     /*void*/ bcopy(one, two, 9);
  600.     equal(two, "hi there", 4);        /* Just paranoia. */
  601.     equal(one, "hi there", 5);        /* Stomped on source? */
  602.  
  603.     /*
  604.      * bzero
  605.      */
  606.     it = "bzero";
  607.     /*void*/ strcpy(one, "abcdef");
  608.     bzero(one+2, 2);
  609.     equal(one, "ab", 1);            /* Basic test. */
  610.     equal(one+3, "", 2);
  611.     equal(one+4, "ef", 3);
  612.  
  613.     /*void*/ strcpy(one, "abcdef");
  614.     bzero(one+2, 0);
  615.     equal(one, "abcdef", 4);        /* Zero-length copy. */
  616.  
  617.     /*
  618.      * bcmp - somewhat like memcmp
  619.      */
  620.     it = "bcmp";
  621.     check(bcmp("a", "a", 1) == 0, 1);    /* Identity. */
  622.     check(bcmp("abc", "abc", 3) == 0, 2);    /* Multicharacter. */
  623.     check(bcmp("abcd", "abce", 4) != 0, 3);    /* Honestly unequal. */
  624.     check(bcmp("abce", "abcd", 4) != 0, 4);
  625.     check(bcmp("alph", "beta", 4) != 0, 5);
  626.     check(bcmp("abce", "abcd", 3) == 0, 6);    /* Count limited. */
  627.     check(bcmp("abc", "def", 0) == 0, 8);    /* Zero count. */
  628.  
  629. }
  630.